/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;
import static org.sourceprojects.lycia.fluent.FluentBuilder.contextObject;
import static org.sourceprojects.lycia.fluent.FluentBuilder.method;
import static org.sourceprojects.lycia.fluent.FluentBuilder.prepare;
import static org.sourceprojects.lycia.fluent.FluentBuilder.tag;
import static org.sourceprojects.lycia.fluent.FluentBuilder.validateSchema;
import static org.sourceprojects.lycia.fluent.FluentBuilder.xpath;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.Child;
import org.sourceprojects.lycia.annotations.ContextObject;
import org.sourceprojects.lycia.annotations.Tag;
import org.sourceprojects.lycia.annotations.TextBody;
import org.sourceprojects.lycia.fluent.FluentBuilder;

public class TermParserTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<matches><synset id=\"matcher\"><term term=\"foo\" /><term term=\"bar\" /><term /></synset></matches>";
	private static final String XML_DATA2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<matches><synset>matcher</synset></matches>";

	@Test
	public void testXmlPathConfigure() throws Exception {
		final Method method = getClass().getMethod("parseData", List.class,
				String.class);

		final FluentBuilder<List<String>> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(xpath("//matches/synset/term[@term]", method(method,
				this)));

		final LyciaParser<List<String>> xmlParser = builder.build();

		xmlParser.getParserContext().setContextObject(new ArrayList<String>());

		xmlParser.parse(XML_DATA);
		final List<String> values = xmlParser.getParserContext()
				.getContextObject();

		assertEquals(2, values.size());
		assertEquals("foo", values.get(0));
		assertEquals("bar", values.get(1));
	}

	@Test
	public void testChildAnnotationSetAttribute() throws Exception {
		final StringBuilder sb = new StringBuilder();

		final FluentBuilder<StringBuilder> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(
				tag("matches", method("parseData", this, StringBuilder.class,
						String.class))).configure(contextObject(sb));

		final LyciaParser<StringBuilder> xmlParser = builder.build();

		xmlParser.parse(XML_DATA);

		assertEquals("matcher", sb.toString());
	}

	@Test
	public void testChildAnnotationSetTextBody() throws Exception {
		final StringBuilder sb = new StringBuilder();

		final FluentBuilder<StringBuilder> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(
				tag("matches", method("parseData2", this, StringBuilder.class,
						String.class))).configure(contextObject(sb));

		final LyciaParser<StringBuilder> xmlParser = builder.build();

		xmlParser.parse(XML_DATA2);

		assertEquals("matcher", sb.toString());
	}

	public void parseData(@ContextObject final List<String> values,
			@Attribute("term") final String value) {

		values.add(value);
	}

	public void parseData(@ContextObject final StringBuilder sb,
			@Child(@Tag("synset")) @Attribute("id") final String value) {

		sb.append(value);
	}

	public void parseData2(@ContextObject final StringBuilder sb,
			@Child(@Tag("synset")) @TextBody final String value) {

		sb.append(value);
	}

}
